package client.comm.baoding.ui

import android.content.Intent
import android.util.Log
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.page.RechargeOrderPageAdapter
import client.comm.baoding.api.bean.BankBind
import client.comm.baoding.databinding.ActivityMeorder2Binding
import client.comm.baoding.ui.vm.MeOrderViewModel
import client.comm.commlib.toast.toast
import com.pay.paytypelibrary.base.OnPayResultListener
import com.pay.paytypelibrary.base.OrderInfo
import com.pay.paytypelibrary.base.PayUtil

class RechargeOrderActivity : BaseMiddleActivity<MeOrderViewModel, ActivityMeorder2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_meorder2
    }

    val adapter: RechargeOrderPageAdapter by lazy {
        RechargeOrderPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)
        binding.vm = viewModel
//        when (intent.getIntExtra("index", 0)) {
//            0 -> {
////                binding.tabWrap.getChildAt(0).isSelected = true
//            }
//            1 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(1, false)
//                }, 10)
//            }
//        }
    }

    override fun observerUI() {
        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("取消成功")
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()
                }
            }
        })

        viewModel.rechargeCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()

                    it.data?.let {

                        if (it.w_type.equals("4") || it.w_type.equals("5") || it.w_type.equals("7")) {
                            var paymentUrl = it?.pay_url
                            val intent = Intent(this, WebActivity::class.java).apply {
                                putExtra("adInfo", paymentUrl)
                                putExtra("title", "支付")
                            }
                            startActivity(intent)
                            finish()
                        }
                    }

                }
            }
        })

    }

}